#!/usr/bin/env python2

from __future__ import absolute_import, division, print_function

import nfldb

tpl_body = '''
The following is a list of statistical categories available on `nfldb`
instances of the [Play](http://pdoc.burntsushi.net/nfldb#nfldb.Play) and
[PlayPlayer](http://pdoc.burntsushi.net/nfldb#nfldb.PlayPlayer) classes.

They are listed here to prevent them from cluttering the API documentation.
Note that this list *complements* the instance variables in the API
documentation.

**Note to contributors**: Do not edit this page manually. It is automatically
generated by the
[nfldb-mk-stat-table](https://github.com/BurntSushi/nfldb/blob/master/scripts/nfldb-mk-stat-table)
script.

### Play statistics

{table_play}


### Player statistics

{table_player}

The following fields are derived statistics computed from a combination
of some of the fields above. They may be used just like regular fields
in searching and sorting criteria.

{table_player_derived}
'''.strip()

tpl_table = '''
Field | Description
--- | ---
{rows}
'''.strip()

tpl_row = '''
{field} | {desc}
'''.strip()


def mk_row(field, desc):
    return tpl_row.format(field=field, desc=desc.replace('\n', ' '))


play_rows, player_rows = [], []
for cat in nfldb.stat_categories.values():
    row = mk_row(cat.category_id, cat.description)
    if cat.category_type == nfldb.Enums.category_scope.player:
        player_rows.append(row)
    else:
        play_rows.append(row)

player_derived_rows = []
for field in nfldb.PlayPlayer._sql_derived:
    var = '_PlayPlayer.%s' % field
    player_derived_rows.append(mk_row(field, nfldb.__pdoc__[var]))

tplay = tpl_table.format(rows='\n'.join(play_rows))
tplayer = tpl_table.format(rows='\n'.join(player_rows))
tdplayer = tpl_table.format(rows='\n'.join(player_derived_rows))

print(tpl_body.format(table_play=tplay, table_player=tplayer,
                      table_player_derived=tdplayer))
